package com.yyxx.vankego;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static de.robv.android.xposed.XposedHelpers.findClass;

/**
 * Created by sx on 2019/8/23.
 */

public class Vanke {
    public static void Init(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        if (loadPackageParam.packageName.equals("com.vanke.activity")) {
            Log.e("sunx", "hook in vanke");
            XposedHelpers.findAndHookMethod("com.stub.StubApp", loadPackageParam.classLoader,
                    "getOrigApplicationContext", Context.class, new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            final Context context = (Context) param.args[0];
                            ClassLoader classLoader = context.getClassLoader();

                            XposedHelpers.findAndHookMethod("com.vanke.activity.module.ZZEContext", classLoader, "k", new XC_MethodHook() {
                                @Override
                                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                    super.afterHookedMethod(param);
                                    final String userToken = (String) param.getResult();
                                    String timestamp = String.valueOf(new Date().getTime() / 1000);
                                    int time1 = Integer.valueOf(timestamp);
                                    final SharedPreferences mPreferences = context.getSharedPreferences("com.vanke.token", 0);
                                    final SharedPreferences.Editor mEditor = mPreferences.edit();
                                    if (!userToken.equals(mPreferences.getString("userToken", "")) && (time1 - mPreferences.getInt("token_time", 0) > 60)) {
                                        mEditor.putInt("token_time", time1);
                                        mEditor.putString("userToken", userToken);
                                        mEditor.commit();

                                    }
                                }
                            });

                        }
                    });
        }
    }
}